package com.ccys.doctor.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.ccys.common.vo.ResponseResultApp;
import com.ccys.db.dto.doctor.drugReaction.AllergiesMakeDto;
import com.ccys.db.dto.doctor.drugReaction.MedicationReactionsMakeDto;
import com.ccys.db.dto.doctor.drugReaction.SaveDrugReactionDto;
import com.ccys.db.entity.TblMemberAllergies;
import com.ccys.db.mapper.TblMedicationReactionMapper;
import com.ccys.db.mapper.TblMemberAllergiesMapper;
import com.ccys.db.vo.doctor.drugReaction.AllergiesMakeVo;
import com.ccys.db.vo.doctor.drugReaction.MedicationReactionsMakeVo;
import com.ccys.db.vo.doctor.drugReaction.SaveDrugReactionVo;
import com.ccys.doctor.service.DrugReactionService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @program: WhiteCoat
 * @description: 药物反应接口
 * @author: ZhangJingCan
 * @create: 2024-02-08 11:33
 **/

@Service
public class DrugReactionServiceImpl implements DrugReactionService {

    @Resource
    private TblMemberAllergiesMapper tblMemberAllergiesMapper;

    @Resource
    private TblMedicationReactionMapper tblMedicationReactionMapper;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResultApp<SaveDrugReactionVo> saveDrugReaction(SaveDrugReactionDto dto) {
        //校验
        if (ObjectUtil.isEmpty(dto.getMember_id())) {
            return ResponseResultApp.error("The member_id field is required.");
        }
        if (ObjectUtil.isEmpty(dto.getAllergies()) || ObjectUtil.isEmpty(dto.getMedication_reactions())) {
            return ResponseResultApp.error("illegal parameter");
        }
        //从dto中获取集合
        List<AllergiesMakeDto> allergiesMakeDtos = dto.getAllergies();
        List<MedicationReactionsMakeDto> medicationReactionsMakeDtos = dto.getMedication_reactions();
        //返回对象
        SaveDrugReactionVo vo = new SaveDrugReactionVo();
        List<AllergiesMakeVo> allergiesMakeVos = new ArrayList<>();
        List<MedicationReactionsMakeVo> medicationReactionsMakeVos = new ArrayList<>();
        //取最小的size
        int total = allergiesMakeDtos.size() > medicationReactionsMakeDtos.size() ? medicationReactionsMakeDtos.size() : allergiesMakeDtos.size();
        Integer index = 1;
        for (int i = 0; i < total; i++) {
            AllergiesMakeDto allergiesMakeDto = allergiesMakeDtos.get(i);
            MedicationReactionsMakeDto reactionsMakeDto = medicationReactionsMakeDtos.get(i);
            //存入数据库的对象
            TblMemberAllergies allergies = new TblMemberAllergies();
            //用户id
            allergies.setMemId(dto.getMember_id());
            //非空才添加
            if (ObjectUtil.isNotEmpty(allergiesMakeDto)) {
                allergies.setAllergiesName(allergiesMakeDto.getName());
            }
            if (ObjectUtil.isNotEmpty(reactionsMakeDto)) {
                allergies.setMedicationReactionId(reactionsMakeDto.getId());
            }
            //添加进数据库
            if (ObjectUtil.isNotEmpty(allergies)) {
                tblMemberAllergiesMapper.insertSelective(allergies);
                //设置返回对象值
                AllergiesMakeVo allergiesMakeVo = new AllergiesMakeVo();
                allergiesMakeVo.setIndex(index);
                //获取添加之后的id
                allergiesMakeVo.setMemAllergiesId(allergies.getId());
                // todo 这个值对方是拿的allergies表的id - 对方传入的dto也有这个id，但是我们没有这个表了  - 这里直接取dto的id
                allergiesMakeVo.setMdAllergiesId(allergiesMakeDto.getId());
                //设置name  - 这里是将字母全部转换为大写 - 对方也是这样
                allergiesMakeVo.setName(allergiesMakeDto.getName().toUpperCase());
                allergiesMakeVos.add(allergiesMakeVo);
                vo.setAllergies(allergiesMakeVos);

                MedicationReactionsMakeVo medicationReactionsMakeVo = new MedicationReactionsMakeVo();
                medicationReactionsMakeVo.setIndex(index);
                //设置id - 因为表合并了，这里和上方获取一样
                medicationReactionsMakeVo.setMemMedicationReactionId(allergies.getId());
                medicationReactionsMakeVo.setMdMedicationReactionId(reactionsMakeDto.getId());
                //名称需要单独查询
                medicationReactionsMakeVo.setName(tblMedicationReactionMapper.findNameById(reactionsMakeDto.getId()));
                medicationReactionsMakeVos.add(medicationReactionsMakeVo);
                vo.setMedicationReactions(medicationReactionsMakeVos);
                //索引++
                index++;
            }
        }

        return ResponseResultApp.success(vo);
    }
}
